class SprintBacklogController < ApplicationController
  before_filter :sprintBacklog_allow_show, :expect => []
  #muss später angepasst werden!
  
  def sprintBacklog_allow_show
    if current_user && current_user.current_project_id != nil# && current_user_role.SprintBacklog == true
      return true
    else
      render :partial => "layouts/denied"
      #redirect_to(:controller => 'sessions', :action => 'new')
      return false
    end
  end
  
  # wird zur Kontrolle aufgerufen und legt wenn abgelaufen einen neuen sprint an
  def new_sprint( project_id )
    project = Project.where("id" => project_id ).first
    sprint = SprintBacklog.where("project_pid" => project_id).last
    sprint_id = sprint.id
    
    ende = sprint.sprint_end
    while DateTime.now > ende
      slaenge = project.sprint_length 
      newsb = SprintBacklog.new
      newsb.sprint_start = ende
      newsb.sprint_end = ende + slaenge*7*24*60*60    
      newsb.project_pid = project.id
      newsb.save     
      project.sprint_id = newsb.id
      project.save
      ende = newsb.sprint_end
      sprint_id = newsb.id
    end
    return sprint_id
  end
  
  def update
    @story = UserStory.find_by_id(params[:id])
    state_id = SprintBacklogColumn.find_by_name(params[:newState]).id
    saved = false
    unless @story.nil? && state_id.nil?
      saved = @story.update_attributes(:sprint_backlog_column_id => state_id)
    end
    
    render :layout => nil, :text => saved
  end
  
=begin
  # Richtiges aktives Projekt muss noch ausgewählt werden! (Aus Select Feld im Sprintbacklog mit übergeben!)
  # wenn ein spezieller sprint geladen werden soll, wird eine id übergeben 
=end
  def show 
    show_sprint_id = params[:id]
    new_project_id = params[:active_project_id]
    current_user.current_project_id = new_project_id
    
    if new_project_id.nil?
      new_project_id = Project.find_by_id(current_user.current_project_id)
    end
    
    if show_sprint_id.nil?    
      show_sprint_id = new_sprint( new_project_id )
      show_sprint_id = SprintBacklog.find_by_id( show_sprint_id ).id
    end
    
    id_im_sprintbacklog = UserStoryState.find_by_status("Im&nbsp;Sprintbacklog").id
    id_stat_finished = SprintBacklogColumn.where("project_id" => new_project_id, "name" => "Fertig").first
    if id_stat_finished.nil?
      id_stat_finished = 0
    else
      id_stat_finished = id_stat_finished.id
    end
    @items = UserStory.joins(:sprint_backlog).where('sprint_backlog_id' => show_sprint_id, 'user_story_state_pid' => id_im_sprintbacklog)
    @sprint = SprintBacklog.where("id"=>show_sprint_id).first
    @spnr = SprintBacklog.where("project_pid" => new_project_id).count  
    status = SprintBacklogColumn.find_all_by_project_id( new_project_id )
    @column = Hash.new
    @sumOfRating = 0
    @sumOfFinished = 0
    status.each_with_index do |stat,i|
      @column[stat.name] = @items.find_all_by_sprint_backlog_column_id( stat.id )   
      ratings = @items.joins(:user_story_rating).select("rating").find_all_by_sprint_backlog_column_id( stat.id )
      ratings.each do |rating|
        @sumOfRating += rating["rating"].to_i
        if stat.id == id_stat_finished
          @sumOfFinished += rating["rating"].to_i
        end
      end
    end
    
    @show_sb_id = show_sprint_id
    
    if !params[:active_project_id].nil? || !params[:id].nil?
      render :layout => nil
    end
  end

  def reset_user_story_rating
    @userStory = UserStory.find_by_id(params[:user_story])
    @userStory.update_attributes(:user_story_rating_id => nil, :user_story_state_pid => 1)
    
    render :layout => nil;
  end

  def update_story
  end
  
    
  def getPieChartData
    
    a = Hash.new
    a["Abgeschlossen"] = @sumOfFinished;
    a["Offen"] = @sumOfTodo;
    a["In--leer--Bearbeitung"] = @sumOfRating;
    return a
  end  
  
  
  def getSprintBacklogLineChartData    
    min = @sprint.sprint_start.to_f
    max = @sprint.sprint_end.to_f
    sum_story_points=0
    @items.each do |us|
      usr = UserStoryRating.find_by_id(us.user_story_rating_id)
      sum_story_points+= usr.rating
    end
    x=0
    b = Hash.new
    
    columns = SprintBacklogColumn.where(:project_id => current_user.current_project_id)
    cmax = columns.first
    columns.each do |c|
      cmax = c if cmax.nil?
      cmax = c if c.column_position > cmax.column_position
    end
    a = []
    a[0]=sum_story_points
    a[1]=sum_story_points
    d = DateTime.current
    d = (min+((max-min)*x/5))
    b[Time.at(d).to_formatted_s(:short)] = a
    x=1
    while x <= 5
      a = []
      a[0]=sum_story_points
      d = (min+((max-min)*x/5))
      @items.each do |us|
        usr = UserStoryRating.find_by_id(us.user_story_rating_id)
        rating = usr.rating
        a[0]-= rating if (us.sprint_backlog_column_id==cmax.id && Time.at(d)>us.updated_at )
      end
      a[1] = sum_story_points-(sum_story_points*(x/5.0))
      #d = DateTime.current
      b[Time.at(d).to_formatted_s(:short)] = a
      x += 1
    end
    
    return b
  end
  helper_method :getPieChartData
  helper_method :getSprintBacklogLineChartData
  
end
